package com.caszx.chargingpile.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.caszx.chargingpile.eventbus.EventBusAction;
import com.caszx.chargingpile.utils.DialogHelp;
import com.caszx.chargingpile.utils.ToastHelp;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import de.greenrobot.event.EventBus;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    //    public static  String WEIXIN_APPID = "wx446ead571d4c4c6d";
    public static String WEIXIN_APPID = "wxe569315893546574";
    private Activity mActivity = this;
    public static IWXAPI api;

    private void handleIntent(Intent paramIntent) {
        api.handleIntent(paramIntent, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this.getApplicationContext(), WEIXIN_APPID);
        api.registerApp(WEIXIN_APPID);
        handleIntent(getIntent());
    }


    @Override
    public void onReq(BaseReq arg0) {
      //  finish();
//        ToastHelp.showMessage(this,"onReq "+arg0.getType());
    }

    @Override
    public void onResp(BaseResp resp) {
        ToastHelp.showMessage(this, "onResp " + resp.errStr);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if(resp.errCode == 0)  {
                DialogHelp.showDialogOnlyMesg(this,"确定",null,"支付成功",null);
                EventBus.getDefault().post(EventBusAction.ACTION_USER_PAY_SUCCESS);
            }else if(resp.errCode  == -2)  {
                DialogHelp.showDialogOnlyMesg(this,"确定",null,"用户取消支付",null);
            }else{
                DialogHelp.showDialogOnlyMesg(this,"确定",null,"支付失败",null);
            }
        }
       // finish();
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }
}
